package com.ruoyi.publish.service.impl;

import java.util.List;

import com.ruoyi.common.annotation.ExcelImportAdvice;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.publish.domain.RvSample;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.publish.mapper.IdLawMapper;
import com.ruoyi.publish.domain.IdLaw;
import com.ruoyi.publish.service.IIdLawService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 执法机构Service业务层处理
 * 
 * @author 张泽望
 * @date 2020-11-02
 */
@Service
public class IdLawServiceImpl implements IIdLawService
{
    private static final Logger log = LoggerFactory.getLogger(IdLawServiceImpl.class);
    @Autowired
    private IdLawMapper idLawMapper;

    /**
     * 查询执法机构
     * 
     * @param id 执法机构ID
     * @return 执法机构
     */
    @Override
    public IdLaw selectIdLawById(String id)
    {
        return idLawMapper.selectIdLawById(id);
    }

    /**
     * 查询执法机构列表
     * 
     * @param idLaw 执法机构
     * @return 执法机构
     */
    @Override
    public List<IdLaw> selectIdLawList(IdLaw idLaw)
    {
        return idLawMapper.selectIdLawList(idLaw);
    }

    /**
     * 新增执法机构
     * 
     * @param idLaw 执法机构
     * @return 结果
     */
    @Override
    public int insertIdLaw(IdLaw idLaw)
    {
        return idLawMapper.insertIdLaw(idLaw);
    }

    /**
     * 修改执法机构
     * 
     * @param idLaw 执法机构
     * @return 结果
     */
    @Override
    public int updateIdLaw(IdLaw idLaw)
    {
        return idLawMapper.updateIdLaw(idLaw);
    }

    /**
     * 删除执法机构对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteIdLawByIds(String ids)
    {
        return idLawMapper.deleteIdLawByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除执法机构信息
     * 
     * @param id 执法机构ID
     * @return 结果
     */
    @Override
    public int deleteIdLawById(String id)
    {
        return idLawMapper.deleteIdLawById(id);
    }

    @Transactional(rollbackFor = BusinessException.class)
    @Override
    public String importIdLaw(List<IdLaw> idLawList, Boolean isUpdateSupport, String operName, String year) {
        if (StringUtils.isNull(idLawList) || idLawList.size() == 0)
        {
            throw new BusinessException("导入案件数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        int orderNum=0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (IdLaw entity : idLawList)
        {
            orderNum++;

            try
            {

                //保存记录
                entity.setCreateDate(DateUtils.getNowDate());
                entity.setCreateName(operName);
                int u = idLawMapper.insertIdLaw(entity);


                // 验证是否保成功
                if (u==1)
                {

                    successNum++;
//                    successMsg.append("<br/>" + successNum + "、账号 " + user.getLoginName() + " 导入成功");
                }
                else
                {
                    failureNum++;
//                    failureMsg.append("<br/>" + failureNum + "、账号 " + user.getLoginName() + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" +  " 导入失败：";
                // failureMsg.append(e.getMessage());
                log.error(msg, e);
            }
            orderNum++;
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new BusinessException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条");
        }
        return successMsg.toString();
    }
}
